package com.lovepick.adapter;

import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lovepick.R;
import com.lovepick.bean.MsgListItem;
import com.lovepick.util.BaseUtils;

public class MsgListItemAdapter extends BaseAdapter
{
	private final String TAG = "MsgListItemAdapter";
	private List<MsgListItem> mMsgLists = null;
	private Context mContext;
	private LayoutInflater mInflater;

	public MsgListItemAdapter(Context context, List<MsgListItem> list)
	{
		Log.i(TAG, "-----MsgListAdapteer-----");
		this.mContext = context;
		this.mMsgLists = list;
		mInflater = LayoutInflater.from(mContext);
	}

	@Override
	public int getCount()
	{
		// TODO Auto-generated method stub
		return mMsgLists.size();
	}

	@Override
	public Object getItem(int position)
	{
		// TODO Auto-generated method stub
		return mMsgLists.get(position);
	}

	@Override
	public long getItemId(int position)
	{
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		// Log.i(TAG, "-----=getView-----=");
		// TODO Auto-generated method stub
		ViewHolder viewHolder = null;
		MsgListItem msgListItem = mMsgLists.get(position);
		if (null == convertView)
		{
			convertView = mInflater.inflate(R.layout.msglist_item, null);
			Log.i(TAG, "new一个viewHolder");
			viewHolder = new ViewHolder();
			viewHolder.imvAvatar = (ImageView) convertView.findViewById(R.id.img_msglist_avatar);
			viewHolder.txvNickName = (TextView) convertView.findViewById(R.id.txv_msglist_from);
			viewHolder.txvMsgNums = (TextView) convertView.findViewById(R.id.txv_msglist_msgnums);
			viewHolder.txvMsgSummary = (TextView) convertView.findViewById(R.id.txv_msglist_summary);
			viewHolder.txvDate = (TextView) convertView.findViewById(R.id.txv_msglist_date);
			convertView.setTag(viewHolder);
		}
		else
		{
			viewHolder = (ViewHolder) convertView.getTag();
		}

		if (msgListItem.getHasNew() == 1)
		{
			convertView.setBackgroundResource(android.R.color.background_light);
			//viewHolder.txvMsgNums.setTextColor(mContext.getResources().getColor(R.color.blue_light));
		}
		else
		{
			//viewHolder.txvMsgNums.setTextColor(Color.BLACK);
			convertView.setBackgroundResource(0);
		}

		viewHolder.txvNickName.setText(msgListItem.getNickName());
		viewHolder.txvMsgNums.setText(String.valueOf(msgListItem.getCount()));
		viewHolder.txvMsgSummary.setText(msgListItem.getLastMsg());
		String date = BaseUtils.getSimpleDate(msgListItem.getLastDate());
		viewHolder.txvDate.setText(date);
		return convertView;
	}

	static class ViewHolder
	{
		public ImageView imvAvatar;
		public TextView txvNickName;
		public TextView txvMsgNums;
		public TextView txvMsgSummary;
		public TextView txvDate;
	}

}
